package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    //查询列表
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        //参数检查
        dto.checkParam();
        //根据名称模糊分页查询
        IPage pageParam = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage page = page(pageParam, wrapper);
        //封装
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    //敏感词新增
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        int count = count(wrapper);
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    //敏感词修改
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive1 = getById(adSensitive.getId());
        if(adSensitive1 == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(StringUtils.isNotBlank(adSensitive.getSensitives()) && !adSensitive.getSensitives().equals(adSensitive1.getSensitives())){
            LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
            int count = this.count(wrapper);
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //参数校验
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = getById(id);
        if(adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {

        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
